import PopoverCore from './popover-core.js'
const popover_map = new Map()

const popover = {
    open(_reference, options) {
        if (!_reference) {
            console.error('reference not found')
            return
        }
        const reference = typeof _reference === 'string' ? document.querySelector(_reference) : _reference

        const popoverId = reference.getAttribute(PopoverCore.dataPopoverIdName)
        let ins = popover_map.get(+popoverId)
        if (ins) {
            ins.show()
            return ins
        } else {
            ins = new PopoverCore(reference, options)
            popover_map.set(ins.popoverId, ins)
            return ins
        }
    }
}

export default popover
